package com.example.pages;

import com.apple.eawt.UserSessionListener;
import com.example.model.SortedListOf;
import com.example.model.User;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;


public class UsersPage extends InternalPage {


    @FindBy(css = "h2")

    public WebElement UserMan;

    @FindBy(name = "permission")
    public WebElement Permission;

    @FindBy(name = "password2")
    public WebElement password2;

    @FindBy(name = "password")
    public WebElement passwordField;

    @FindBy(name = "username")
    public WebElement userNameField;

    @FindBy(name = "email")
    public WebElement emailField;

    @FindBy(name = "submit")
    public WebElement SaveButton;

    //начнем искать пользователей
    @FindBy(css = "div#user-list table > tbody >tr")
    public List<WebElement> userRows;
    ;


    public boolean isOnManagePage() {
        return UserMan.isDisplayed();
    }

    public UsersPage addGuestUser(User user) {
        userNameField.clear();
        userNameField.sendKeys(user.name);
        passwordField.clear();
        passwordField.sendKeys(user.password);
        password2.sendKeys(user.password);
        emailField.clear();
        emailField.sendKeys(user.email);
        SaveButton.click();
        cachedUsers = null;
        return MyPageFactory.getPage(driver, UsersPage.class);
    }


    public UserPage deleteUserGoodTest() {
        SortedListOf<User> oldUserList = getUsers();
        User user = new User()
                .withName("user1").withEmail("user1@mail.com").withPassword("1234");
        if (getUserCount() == 1) {

            addGuestUser(user);
            oldUserList = getUsers();
        }

        //    return MyPageFactory.getPage(driver, UserPage.class);
        deleteUser(user);
        SortedListOf<User> newUserList = getUsers();
            assertEquals(oldUserList.without(user), newUserList);
            cachedUsers = null;
            return MyPageFactory.getPage(driver, UserPage.class);
        }



    public UsersPage deleteUser(User user) {
        for (WebElement row : userRows) {
            if (user.equals(convertRowToUser(row))) {
                row.findElement(By.xpath("./td[6]/a")).click();
                wait_().until(alertIsPresent()).accept();
                break;
            }

        }
        cachedUsers = null;
        return MyPageFactory.getPage(driver, UsersPage.class);

    }
    //специальный кеш

    private SortedListOf<User> cachedUsers;

    public SortedListOf<User> getUsers() {
        if (cachedUsers == null) {
            cachedUsers = new SortedListOf<User>();
            for (WebElement row : userRows) {
                cachedUsers.add(convertRowToUser(row));
            }
        }
        return new SortedListOf<User>(cachedUsers);
    }


    public int getUserCount() {
        return userRows.size();
    }


    private User convertRowToUser(WebElement row) {
        List<WebElement> cells = row.findElements(By.tagName("td"));
        return new User()
                .withName(cells.get(1).getText())
                .withEmail(cells.get(2).getText())
                .withRole(cells.get(2).getText());

    }


}




